package graphics.pixelbender.src 
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.ShaderFilter;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import utils.GeomUtils;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class NormalizedVariations extends BaseSample
	{
		
		[Embed(source='../../../../lib/chatquilouche2.jpg')]private var src:Class;
		private var image:Bitmap;
		
		[Embed(source='../pbj/normalized_variations.pbj', mimeType='application/octet-stream')]private var shaderPBJ:Class;
		private var shader:Shader;
		private var filter:ShaderFilter;
		private var delta:Number = 0;
		private var theta:Number = 1;
		
		public function NormalizedVariations() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage.addEventListener( MouseEvent.MOUSE_WHEEL, mouseWheelHandler );
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler );
			
			shader = new Shader(  new shaderPBJ() );
			filter = new ShaderFilter( shader );
			
			image = new Bitmap( Bitmap( new src() ).bitmapData );
			addChild( image );
			
			center();
			
			interval = setInterval( update, 30 );
			
		}
		
		private function keyHandler(e:KeyboardEvent):void 
		{
			if( e.keyCode == Keyboard.SHIFT ) theta = 0;
			if( e.keyCode == Keyboard.CONTROL ) theta = 1;
		}
		
		private function mouseWheelHandler(e:MouseEvent):void 
		{
			delta += int( e.delta / 3 );
		}
		
		private function update( e:Event = null  ) : void
		{
			//trace( int( delta ) )
			shader.data.center.value = [ mouseX, mouseY ];
			shader.data.mode.value = [ int( delta ) ];
			shader.data.feedback.value = [ int( theta ) ];
			image.filters = [ filter ];
			
		}
	}
}